package cn.lger.controller;

import cn.lger.pojo.Article;
import cn.lger.pojo.User;
import cn.lger.service.ArticleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Code that Changed the World
 *
 * @author Pro
 * @date 2018-06-14.
 */
@Controller
@Api(tags = "视图控制器")
public class ViewController {


    @Resource
    private ArticleService articleService;
    @Resource
    private ObjectMapper objectMapper;

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/doc")
    public String doc() {
        return "doc";
    }

    @GetMapping("movie")
    @ApiOperation("返回电影解析页面")
    public String movie(@RequestParam @ApiParam("电影链接") String url, @ApiIgnore Model model) {
        model.addAttribute("url", url);
        return "movie";
    }

    @GetMapping("article/{id}")
    @ApiOperation("返回电影影评文章页面")
    public String article(@PathVariable @ApiParam("影评文章id") int id, @ApiIgnore Model model) {
        model.addAttribute("article", articleService.findOneById(id));
        return "article";
    }

    @GetMapping("contact")
    @ApiOperation("返回联系反馈页面")
    public String contact() {
        return "contact";
    }

    @GetMapping("articles")
    @ApiOperation("返回影评文章列表页面")
    public String articles() {
        return "articles";
    }


    @GetMapping("user/center")
    @ApiOperation("返回影评用户中心页面")
    public String center() {
        return "center";
    }

    @GetMapping("user/article/{id}")
    @ApiOperation("返回用户影评文章编辑页面，如果id不为空，Request域加入article对象，jsp直接可以取出")
    public String updateArticle(@PathVariable int id, @ApiIgnore Model model, @ApiIgnore HttpSession session) {
        Object o = session.getAttribute("user");
        User user;
        if (o instanceof User) {
            user = (User) o;
        } else {
            throw new IllegalStateException("用户未登录，请登录后访问");
        }
        Article article = articleService.findOneByArticleIdAndUserId(id, user.getId());
        if (article == null) {
            return "edit";
        }
        model.addAttribute("article", article);
        return "edit";
    }

    @GetMapping("user/article/{id}.json")
    @ApiOperation("返回用户影评文章编辑页面，如果id不为空，Request域加入article对象，jsp直接可以取出")
    public String updateArticleJson(@PathVariable int id, @ApiIgnore Model model, @ApiIgnore HttpSession session) {
        Object o = session.getAttribute("user");
        User user;
        if (o instanceof User) {
            user = (User) o;
        } else {
            throw new IllegalStateException("用户未登录，请登录后访问");
        }
        Article article = articleService.findOneByArticleIdAndUserId(id, user.getId());
        if (article == null) {
            model.addAttribute("article", "");
            return "edit";
        }
        try {
            model.addAttribute("article", objectMapper.writeValueAsString(article));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "edit";
    }

    @GetMapping("user/article")
    @ApiOperation("返回用户影评文章编辑页面")
    public String addArticle(@ApiIgnore HttpSession session) {
        Object o = session.getAttribute("user");
        if (!(o instanceof User)) {
            throw new IllegalStateException("用户未登录，请登录后访问");
        }
        return "edit";
    }

    @GetMapping("admin")
    @ApiOperation("返回管理员管理页面")
    public String adminView(){
        return "admin";
    }

}
